//
//  RefundTicketViwew.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/12.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class RefundTicketView: UIView {
    
    public var orderInfos:[OrderInfo]? {
        didSet {
            updateLayout()
        }
    }
    
    public var passengers:[PassengerInfo]? {
        didSet {
            updateLayout()
        }
    }
    
    // 滑动
    private var scrollview = UIScrollView()
    // 没有更多数据
    private let nomoredata = NoMoreDataView()
    // 回调
    public var complete:((_ item:[String]) -> Void)?
    
    // 存储对勾图片
    public var imgviews = [UIImageView]()
    
    override func layoutSubviews() {
        guard let orderInfos = orderInfos else {
            return
        }
        guard let passengers = passengers else {
            return
        }
        // 高度
        var tempHeight:CGFloat = 0
        // 开始布局
        for i in 0..<orderInfos.count {
            let changeView = SubChangeTicketView()
            changeView.tag = i
            changeView.frame = CGRect(x: MARGIN, y: tempHeight, width: frame.width - MARGIN * 2, height: 240)
            changeView.layer.cornerRadius = 5
            changeView.subOrderInfo = orderInfos[i]
            changeView.subPassenger = passengers[i]
            changeView.backgroundColor = UIColor.white
            scrollview.addSubview(changeView)
            // 对勾图片
            let imgview = UIImageView(image: UIImage(named: "refund0"))
            imgview.tag = i
            let imgX = changeView.frame.width - imgview.bounds.width + MARGIN
            let imgY = changeView.frame.height - imgview.bounds.height + tempHeight
            imgview.frame.origin = CGPoint(x: imgX, y: imgY)
            scrollview.addSubview(imgview)
            imgviews.append(imgview)
            // 记录最后一个View高度
            tempHeight = changeView.frame.maxY + MARGIN10
            // 添加点击事件
            changeView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(ui:))))
        }
        // 没有更多数据
        nomoredata.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: MARGIN40)
        scrollview.addSubview(nomoredata)
        // 设置scrollview属性
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollview.contentSize = CGSize(width: frame.width, height: nomoredata.frame.maxY + MARGIN20)
        addSubview(scrollview)
    }
    
    // 所有选中的Item的订单号
    func selectItemOrderids() -> [String] {
        if imgviews.count == 0 {
            return [String]()
        }
        guard let orders = orderInfos else {
            return [String]()
        }
        var temp = [String]()
        for img in imgviews {
            let order = orders[img.tag]
            if img.image == UIImage(named: "refund1") {
                temp.append(order.orderid!)
            }
        }
        return temp
    }
    
    
    @objc func clickItem(ui:UITapGestureRecognizer) -> Void {
        if imgviews.count == 0 {
            return
        }
        guard let index = ui.view?.tag else {
            return
        }
        let img = imgviews[index]
        if img.image == UIImage(named: "refund0") {
            img.image = UIImage(named: "refund1")
        }else {
            img.image = UIImage(named: "refund0")
        }
        if let callback = complete {
            callback(self.selectItemOrderids())
        }
    }
    
    class SubChangeTicketView: UIView {
        
        public var subOrderInfo:OrderInfo?
        public var subPassenger:PassengerInfo?
        
        override func layoutSubviews() {
            guard let ticket = subOrderInfo else {
                return
            }
            // 日期
            let symbol = UILabel()
            symbol.textColor = COLOR_NICE
            symbol.text = AirLineUtil.shareInstance.getConvertDate(time: ticket.departdate!, isYear: true)
            symbol.font = UIFont.systemFont(ofSize: 13)
            symbol.frame = CGRect(x: MARGIN20, y: MARGIN , width: MARGIN140, height: MARGIN)
            addSubview(symbol)
            let changeTicket = UILabel(title: "改签票", color: UIColor.gray, size: 11, align: .center)
            changeTicket.layer.cornerRadius = 3
            changeTicket.layer.borderWidth = 1
            changeTicket.layer.borderColor = UIColor.gray.cgColor
            let changeX = (frame.width - MARGIN40) * 0.5
            changeTicket.frame = CGRect(x: changeX, y: 13, width: MARGIN50, height: MARGIN20)
            if ticket.changorder == 1 {
                addSubview(changeTicket)
            }
            // 票价
            let cabinname = UILabel()
            cabinname.textAlignment = .right
            cabinname.text = "¥\(ticket.ticketprice)"
            cabinname.font = UIFont.systemFont(ofSize: 16)
            cabinname.textColor = COLOR_RED
            cabinname.frame = CGRect(x: frame.width - 170 - MARGIN20, y: MARGIN, width: 170, height: MARGIN)
            addSubview(cabinname)
            // 出发时间
            let startTime = UILabel()
            startTime.text = ticket.departtime
            startTime.textColor = UIColor.black
            let startTimeY = symbol.frame.maxY + MARGIN20
            startTime.font = UIFont.boldSystemFont(ofSize: 25)
            startTime.frame = CGRect(x: MARGIN20, y:  startTimeY, width: MARGIN80, height: MARGIN30)
            addSubview(startTime)
            // 到达时间
            let stopTime = UILabel()
            stopTime.text = ticket.arrivaltime
            stopTime.textAlignment = .right
            stopTime.textColor = UIColor.black
            stopTime.font = UIFont.boldSystemFont(ofSize: 25)
            stopTime.frame = CGRect(x: frame.width - MARGIN100, y: startTimeY , width: MARGIN80, height: MARGIN30)
            addSubview(stopTime)
            // 箭头图片
            let arrowPic = UIImageView(image: UIImage(named: "checi_arrow"))
            arrowPic.frame.origin = CGPoint(x: center.x - arrowPic.frame.width * 0.5, y:startTimeY + MARGIN25 )
            addSubview(arrowPic)
            // 飞行时长
            let miletime = UILabel()
            miletime.text = ticket.miletime
            miletime.textColor = UIColor.gray
            let mileTextWidth = miletime.text!.boundingRect(w: MARGIN50, h: MARGIN, fontSize: 13).width * 0.5
            let offset = arrowPic.bounds.size.width * 0.5 - mileTextWidth
            miletime.font = UIFont.systemFont(ofSize: 13)
            let miletimeX = center.x - arrowPic.frame.width * 0.5 + offset
            miletime.frame = CGRect(x: miletimeX, y: arrowPic.frame.maxY - 18, width: MARGIN50, height: MARGIN)
            addSubview(miletime)
            // 出发航站楼
            let departTime = UILabel()
            departTime.textColor = COLOR_NICE
            departTime.text = "\(ticket.city!)机场 \(ticket.departterminal!)"
            departTime.font = UIFont.systemFont(ofSize: 13)
            departTime.frame = CGRect(x: MARGIN20, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(departTime)
            // 到达航站楼
            let arrivalTime = UILabel()
            arrivalTime.textAlignment = .right
            arrivalTime.textColor = COLOR_NICE
            arrivalTime.text = "\(ticket.endcity!)机场 \(ticket.arrivalterminal!)"
            arrivalTime.font = UIFont.systemFont(ofSize: 13)
            let arrivalTimeX = frame.width - MARGIN140 - MARGIN20
            arrivalTime.frame = CGRect(x: arrivalTimeX, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(arrivalTime)
            // 航班信息
            let airLineInfo = UILabel()
            airLineInfo.textColor = COLOR_NICE
            airLineInfo.text = "\(ticket.flightnumberid!) | \(ticket.cabinname!)  | \(ticket.craft!) | \(ticket.food!) | 里程:\(Int(ticket.mileage))KM"
            airLineInfo.font = UIFont.systemFont(ofSize: 12)
            let airLineInfoY = departTime.frame.maxY + MARGIN20
            airLineInfo.frame = CGRect(x: MARGIN20, y: airLineInfoY, width: SCREEN_WIDTH - MARGIN40, height: MARGIN)
            addSubview(airLineInfo)
            // 下划线
            let lineY = airLineInfo.frame.maxY + MARGIN
            let line = UIView(frame: CGRect(x: MARGIN20, y: lineY, width: frame.width - MARGIN40, height: 1))
            line.backgroundColor = COLOR_LINE
            addSubview(line)
            // 乘机人文本
            let passengerY = line.frame.maxY + MARGIN5
            let passenger = UILabel(title: "乘机人", color: COLOR_NICE, size: 13)
            passenger.frame = CGRect(x: MARGIN20, y: passengerY, width: MARGIN100, height: MARGIN40)
            addSubview(passenger)
            // 乘机人
            let username = UILabel(title: "", color: UIColor.gray, size: 13,align: .right)
            username.text = subPassenger?.username
            let usernameW = frame.width - passenger.frame.maxX - MARGIN20
            username.frame = CGRect(x: passenger.frame.maxX, y: passengerY, width: usernameW, height: MARGIN40)
            addSubview(username)
            // 手机号文本
            let mobileView = UILabel(title: "手机号", color: COLOR_NICE, size: 13)
            let mobileY = passenger.frame.maxY
            mobileView.frame = CGRect(x: MARGIN20, y: mobileY, width: MARGIN100, height: MARGIN20)
            addSubview(mobileView)
            // 手机号
            let mobileLabel = UILabel(title: "", color: UIColor.gray, size: 13,align: .right)
            mobileLabel.text = subPassenger?.mobile
            mobileLabel.frame = CGRect(x: passenger.frame.maxX, y: mobileY, width: usernameW, height: MARGIN20)
            addSubview(mobileLabel)
        }
    }
}
